﻿using EdiNet;
using Xunit;
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;

namespace EdiNet.Test
{

    public class EdiXmlReaderWriterCrossTest
    {
        [Fact()]
        public void EdiXmlReaderWriterTest()
        {
            string fn2 = Path.GetTempFileName();
            Stream ediStream = StreamHelper.GetResourceStream(EdiStreamReaderTest.ResourceBasePath, "T.txt");
            Stream ediOutStream = File.Create(fn2);
            EdiXmlReader rd = new EdiXmlReader(ediStream);
            EdiReaderWriterSettings s = EdiReaderWriterSettings.CreateFromChars(DefaultEdiEncoding.Encoding,
                '*', '\0', '>', '~', '\0', SegmentTerminatorSuffix.None, EdiStandard.X12);
            EdiXmlWriter wr = new EdiXmlWriter(ediOutStream, s);

            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(XmlReader.Create(StreamHelper.GetResourceStream("EdiXmlReaderWriterCrossTest", "Copy.xslt")));

            transform.Transform(rd, wr);

            rd.Close();
            wr.Close();

            using (StreamReader s1 = new StreamReader(StreamHelper.GetResourceStream(EdiStreamReaderTest.ResourceBasePath, "T.txt")), s2 = new StreamReader(fn2))
            {
                string content1 = s1.ReadToEnd();
                string content2 = s2.ReadToEnd();
                Assert.Equal(content1, content2);
            }
        }
    }
}
